package com.card.manager.services.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.card.framework.mybatis.mapper.BatchCallBack;
import com.card.framework.mybatis.mapper.MyFrameworkSupperMapper;
import com.card.framework.mybatis.optimist.ModifiedByAnotherUserException;
import com.card.framework.page.model.PageModel;
import com.card.manager.dao.MsaMapper;
import com.card.manager.domain.Msa;
import com.card.manager.domain.MsaExample;
import com.card.manager.domain.MsaExample.Criteria;
import com.card.manager.services.MsaService;

@Service("MsaServiceImpl")
public class MsaServiceImpl extends MyFrameworkSupperMapper implements MsaService{
	
	public Map<String, Object> selectMsaForPage(Msa msa,PageModel pageModel) {
		MsaMapper mapper = this.getMapper(MsaMapper.class);
		MsaExample msaExample = new MsaExample();
		//aaaExample.createCriteria().andUsernameIsNotNull();
		//aaaExample.createCriteria().andUsernameEqualTo(msa.getUsername());
		msaExample.setPage(pageModel);
		if(StringUtils.isNotEmpty(pageModel.getSort())) 
			msaExample.setOrderByClause(pageModel.getSort()+" "+pageModel.getOrder());
		if(StringUtils.isNotEmpty(msa.getNamea())) 
			msaExample.createCriteria().andNameaEqualTo(msa.getNamea());
		if(StringUtils.isNotEmpty(msa.getNamek())) 
			msaExample.createCriteria().andNamekEqualTo(msa.getNamek());
		//
		msaExample.createCriteria().andDelflgEqualTo((byte)1);
		
		List<Msa> list = mapper.selectByExample(msaExample);
		Map<String, Object> modelMap = new HashMap<String, Object>();  
	    modelMap.put("total", pageModel.getTotalCount());  
	    modelMap.put("rows", list); 
	    return modelMap;
	}
	
	@Override
	public void deleteMsas(final List<Msa> msa) throws ModifiedByAnotherUserException,Exception {
		batchSubmit(MsaMapper.class,new BatchCallBack() {
			@Override
			public <E> void batchCallBack(E mapper) {
				if(msa != null && msa.size() > 0) {
					for(Msa a :msa){
						a.setDelflg((byte)0);
						((com.card.manager.dao.MsaMapper) mapper).updateByPrimaryKeySelective(a);
					}
				}
			}
		});
	}

	@Override
	public void addOrUpdate(Msa msa) {
		MsaMapper mapper = this.getMapper(MsaMapper.class);
		//Msa queryResult = mapper.selectByPrimaryKey(msa.getUserid());
		if("1".equals(msa.getOper())){
			msa.setDelflg((byte)1);
			mapper.insert(msa);
		}else
		{
			mapper.updateByPrimaryKeySelective(msa);
		}
	}

	@Override
	public List<Msa> selectMsaByKindId(String kindId){
		MsaMapper mapper = this.getMapper(MsaMapper.class);
		MsaExample msaExample = new MsaExample();
		//aaaExample.createCriteria().andUsernameIsNotNull();
		//aaaExample.createCriteria().andUsernameEqualTo(msa.getUsername());
		Criteria c = msaExample.createCriteria();
		c.andKindidEqualTo(kindId);
		c.andDelflgEqualTo((byte)1);
		List<Msa> list = mapper.selectByExample(msaExample);
	    return list;
	}

}